home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / lib / tcl / mxedit.search < prev    next >
Encoding:
Text File  |  1992-07-17  |  5.7 KB  |  245 lines

  1. # mxedit.search --
  2. #    Definitions for the mxedit search/replace entry
  3. #
  4. # Copyright (c) 1992 Xerox Corporation.
  5. # Use and copying of this software and preparation of derivative works based
  6. # upon this software are permitted. Any distribution of this software or
  7. # derivative works must comply with all applicable United States export
  8. # control laws. This software is made available AS IS, and Xerox Corporation
  9. # makes no warranty about the software, its performance or its conformity to
  10. # any specification.
  11.  
  12. # File Globals
  13. # find - the name of the search/replace entry widget
  14. # findParent - needed to repack the find widget when making it visible
  15. #    We assume this is the last child widget of the parent (pack append)
  16. # searchState - whether or not the search entry is visible
  17.  
  18. # Imported Globals
  19. # focus - used to change the focus after a search command
  20. # paleBackground - color for frame
  21.  
  22. global searchState
  23. set searchState noFindTool
  24.  
  25. # makeFindTool --
  26. #    This makes the search/replace entries
  27.  
  28. proc makeFindTool { parent } {
  29.     global paleBackground
  30.     global find findParent
  31.     set find [selfName $parent ".find"]
  32.     set findParent $parent
  33.     frame $find -background $paleBackground -relief sunken -borderwidth 2
  34.     packFindTool
  35.     labeledEntry $find .target "Search:"  5 { left expand fillx frame w }
  36.     searchBindings $find.target.entry
  37.     labeledEntry $find .replace "Replace:"  5 { right expand fillx frame w }
  38.     replaceBindings $find.replace.entry
  39. }
  40.  
  41. # packFindTool --
  42. proc packFindTool {} {
  43.     global find findParent
  44.     pack append $findParent $find { bottom fillx }
  45. }
  46.  
  47. # hideFindTool --
  48. proc hideFindTool { } {
  49.     global find findParent
  50.     pack unpack $find
  51. }
  52.  
  53. # find --
  54. #    This ensures that the find window is visible
  55. #    The find window can be either a popUp or a packed set of entries.
  56.  
  57. proc find { } {
  58.     global focus
  59.     global find
  60.  
  61.     showFind
  62.     focus $find.target.entry
  63.     set focus $find.target.entry
  64.     $find.target.entry delete 0 end
  65. }
  66. proc findClear { } { find }
  67.  
  68. # searchFocus --
  69. #    Set the focus on the search entry
  70.  
  71. proc searchFocus { } {
  72.     global find
  73.     if [catch {focus $find.target.entry}] {
  74.     showFind
  75.     }
  76. }
  77.  
  78. # replaceClear --
  79. #    Clear the replace entry and move the focus there
  80.  
  81. proc replaceClear { } {
  82.     global find
  83.     showFind
  84.     focus $find.replace.entry
  85.     set focus $find.replace.entry
  86.     $find.replace.entry delete 0 end
  87. }
  88.  
  89. # replaceFocus --
  90. #    Focus on the replace entry
  91.  
  92. proc replaceFocus { } {
  93.     global find
  94.     if [catch {focus $find.replace.entry}] {
  95.     showFind
  96.     }
  97. }
  98.  
  99. # showFind --
  100. #    Make sure the find window is visible
  101. proc showFind { } {
  102.     global searchState
  103.     global find 
  104.     case $searchState in {
  105.     noFindTool {
  106.         set searchState findTool
  107.         makeFindTool "."
  108.         focus $find.target.entry
  109.         return
  110.     }
  111.     findToolHidden {
  112.         set searchState findTool
  113.         packFindTool
  114.         focus $find.target.entry
  115.         return
  116.     }
  117.     noPopUp {
  118.         set searchState popUp
  119.         makePopUp
  120.         focus $find.target.entry
  121.         return
  122.     }
  123.     popUpHidden {
  124.         set searchState popUp
  125.         wm deiconify $find
  126.         placePopUp $find off
  127.         focus $find.target.entry
  128.         return
  129.     }
  130.     default {
  131. #        mxeditFocus
  132.     }
  133.     }
  134. }
  135.  
  136. # hideFind --
  137. #    Hide the find window
  138.  
  139. proc hideFind {} {
  140.     global searchState
  141.     case $searchState in {
  142.     popUp {
  143.         set searchState popUpHidden
  144.         hidePopUp
  145.     }
  146.     findTool {
  147.         set searchState findToolHidden
  148.         hideFindTool
  149.     }
  150.     }
  151. }
  152.  
  153. # findInner --
  154. #    This makes calls to the search and replace mxedit functions
  155. #    Because it is invoked from menus, it also makes sure that
  156. #    the find window is visible
  157.  
  158. proc findInner { op }  {
  159.     global searchState
  160.     global find
  161.  
  162.     # If we are not searching for the selection,
  163.     # then make sure the find tool is visible
  164.     if { ! [string match {[fb]*Sel} $op] } {
  165.     showFind
  166.     set target [$find.target.entry get]
  167.     if {[llength "$target"] == 0} {
  168.         focus $find.target.entry
  169.         mxFeedback "Enter search string"
  170.         return
  171.     }
  172.     }
  173.     if [string match {repl*} $op] {
  174.     set replace [$find.replace.entry get]
  175.     if {[llength "$replace"] == 0} {
  176.         focus $find.replace.entry
  177.         mxFeedback "Enter replace string"
  178.         return
  179.     }
  180.     }
  181.     mxeditFocus
  182.     catch {
  183.     case $op in {
  184.         forwSel {
  185.         applyToSelection {search forward}
  186.         }
  187.         backSel {
  188.         applyToSelection {search backward}
  189.         }
  190.         forward {
  191.         search forward $target
  192.         }
  193.         backward {
  194.         search backward $target
  195.         }
  196.         replace {
  197.         replace selection $replace
  198.         }
  199.         replaceSel {
  200.         replace range sel.left sel.right $target $replace
  201.         }
  202.         replaceEverywhere {
  203.         replace range 1.0 [mark eof] $target $replace
  204.         }
  205.     }
  206.     } msg
  207.     mxFeedback $msg
  208. }
  209.  
  210. # makePopUp --
  211. #    This creates a search/replace dialog box
  212. #    (THIS IS UNUSED - it should be moved into a separate
  213. #    application that can be aimed at any mxedit window)
  214.  
  215. proc makePopUp { } {
  216.     global paleBackground
  217.     global file
  218.     global find
  219.  
  220.     set find ".find"
  221.     toplevel $find -background $paleBackground -relief flat
  222.     wm title $find "Find in $file"
  223.     wm transient $find
  224.  
  225.     buttonFrame $find .buttons
  226.     packedButton $find.buttons .forward "Forward"     { findInner forward }
  227.     packedButton $find.buttons .backward "Backward"     { findInner backward }
  228.     packedButton $find.buttons .replace "Replace"     { findInner replace }
  229.     packedButton $find.buttons .replaceSel "Rep. in Select."     { findInner replaceSel }
  230.     packedButton $find.buttons .quit "Dismiss"     { hidePopUp } right
  231.     labeledEntry $find .target "Search for:"  42 { top frame w }
  232.     searchBindings $find.target.entry
  233.     labeledEntry $find .replace "Replace with:"  40 { bottom frame w }
  234.     replaceBindings $find.replace.entry
  235.  
  236.     placePopUp $find off
  237. }
  238. proc hidePopUp {} {
  239.     global searchState
  240.     global find
  241.     set searchState popUpHidden
  242.     wm withdraw $find
  243. }
  244.  
  245.